package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.Pool;
import com.ruoyi.system.mapper.PoolMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TopicMapper;
import com.ruoyi.system.domain.Topic;
import com.ruoyi.system.service.ITopicService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

import static com.ruoyi.common.utils.ShiroUtils.getLoginName;

/**
 * 题目管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-12
 */
@Service
public class TopicServiceImpl implements ITopicService
{
    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private PoolMapper poolMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TopicServiceImpl.class);

    /**
     * 导入题库
     *
     * @param pool            题库对象
     * @return 结果
     */
    @Override
    public String importTopic(Pool pool) {
        List<Topic> topicList = pool.getTopicList();
        if (StringUtils.isNull(topicList) || topicList.size() == 0) {
            throw new ServiceException("导入题库不能为空！");
        }
        for (Topic topic: topicList){
            if (topic == null){
                throw new ServiceException("sheet列错误");
            }
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Topic topic : topicList) {
            try {
                BeanValidators.validateWithException(validator, topic);
                topic.setCreateBy(getLoginName());
                topic.setCreateTime(DateUtils.getNowDate());
                topic.setPoolId(pool.getPoolId());
                this.insertTopic(topic);
                successNum++;
                successMsg.append("<br/>" + successNum + "、题目 " + topic.getQuestion() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、题目 " + topic.getQuestion() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



    /**
     * 查询题目管理
     *
     * @param topicId 题目管理主键
     * @return 题目管理
     */
    @Override
    public Topic selectTopicByTopicId(Long topicId)
    {
        return topicMapper.selectTopicByTopicId(topicId);
    }

    /**
     * 查询题目管理列表
     *
     * @param topic 题目管理
     * @return 题目管理
     */
    @Override
    public List<Topic> selectTopicList(Topic topic)
    {
        return topicMapper.selectTopicList(topic);
    }

    /**
     * 新增题目管理
     *
     * @param topic 题目管理
     * @return 结果
     */
    @Override
    public int insertTopic(Topic topic)
    {
        topic.setCreateTime(DateUtils.getNowDate());
        topic.setCreateBy(getLoginName());
        Pool pool = new Pool();
        pool.setPoolId(topic.getPoolId());
        pool.setUpdateBy(getLoginName());
        pool.setUpdateTime(DateUtils.getNowDate());
        poolMapper.updatePool(pool);
        return topicMapper.insertTopic(topic);
    }

    /**
     * 修改题目管理
     *
     * @param topic 题目管理
     * @return 结果
     */
    @Override
    public int updateTopic(Topic topic)
    {
        topic.setUpdateTime(DateUtils.getNowDate());
        topic.setUpdateBy(getLoginName());
        Pool pool = new Pool();
        pool.setPoolId(topic.getPoolId());
        pool.setUpdateBy(getLoginName());
        pool.setUpdateTime(DateUtils.getNowDate());
        poolMapper.updatePool(pool);
        return topicMapper.updateTopic(topic);
    }

    /**
     * 批量删除题目管理
     *
     * @param topicIds 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteTopicByTopicIds(String topicIds)
    {
        return topicMapper.deleteTopicByTopicIds(Convert.toStrArray(topicIds));
    }

    /**
     * 删除题目
     *
     * @param topicId 题目主键
     * @return 结果
     */
    @Override
    public int deleteTopicByTopicId(Long topicId)
    {
        Pool pool = new Pool();
        Topic topic = topicMapper.selectTopicByTopicId(topicId);
        pool.setPoolId(topic.getPoolId());
        pool.setUpdateBy(getLoginName());
        pool.setUpdateTime(DateUtils.getNowDate());
        poolMapper.updatePool(pool);
        return topicMapper.deleteTopicByTopicId(topicId);
    }
}
